﻿using EntytiBookShop.DAL;
using EntytiBookShop.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EntytiBookShop
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btShowCAtegories_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            using (BookShopContext ctx = new BookShopContext())
            {
                //ctx.Categories.Add(new Category() { CategoryName = "Ababa"});
                List<Category> Categories = ctx.Categories.ToList();
                foreach (Category cat in Categories)
                {   

                    listBox1.Items.Add(new ListItem(cat.CategoryID, cat.CategoryName));
                }
            }
        }

        private void btAddCategory_Click(object sender, EventArgs e)
        {
            using (BookShopContext ctx = new BookShopContext())
            {
                Category cat = ctx.Categories.Add(new Category() { CategoryName = tbAddCategory.Text });
                ctx.SaveChanges();
                listBox1.Items.Add(new ListItem(cat.CategoryID, cat.CategoryName));
            }

        }

        private void btAddAuthor_Click(object sender, EventArgs e)
        {
            using (BookShopContext ctx = new BookShopContext())
            {
                ctx.Authors.Add(new Author() { FirstName = "Ivan", LastName = "Bunin", BirthDate = Convert.ToDateTime("10/10/1870"), DeathDate = Convert.ToDateTime("8/11/1953"), MiddleName = "Alexeevych", NickName = "Bunin" });
                ctx.SaveChanges();
            }
        }

        private void createToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddBookForm addBookForm = new AddBookForm();
            addBookForm.Show();
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddAuthorForm addAthorForm = new AddAuthorForm();
            addAthorForm.Show();
        }
    }
    public class ListItem
    {
        public int ID { get; set; }
        public string Value { get; set; }

        public ListItem(int id, string value)
        {
            ID = id;
            Value = value;
        }
        public override string ToString()
        {
            return Value+" ("+ID+")";
        }
    }
}
